from cherrypy.process.plugins import SimplePlugin

class BackgroundTaskQueue(SimplePlugin):
    
    thread = None
    
    def __init__(self, bus, queue, qwait=2, safe_stop=True):
        SimplePlugin.__init__(self, bus)
        self.q = queue
        self.qwait = qwait
        self.safe_stop = safe_stop
    
    def start(self):
        self.running = True
    
    def stop(self):
        if self.safe_stop:
            self.running = "draining"
        else:
            self.running = False
        #Need to find out how to stop a process from the queue.
        
        self.running = False
   
    def run(self):
        return
         
    def put(self, func, *args, **kwargs):
        """Schedule the given func to be run."""
        print func
        self.q.put((func, args, kwargs))
        #print self.q.get(block=True, timeout=2)
